<?php if(!defined('BASEPATH')) exit('No direct script allowed');

class mdl_mcm extends CI_Model{
	
	function __construct()
	{
		parent::__construct();
	}
	function kategori()	{		$this->db->select('*');		$this->db->from('master_kategori');		$this->db->order_by('id');  		return $this->db->get();	}		function parawaba()	{		$this->db->select('*');		$this->db->from('master_parawaba');		$this->db->order_by('nama','asc');  		return $this->db->get();	}		function get_mcm($num=0, $offset=0)	{		$this->db->select('*');		$this->db->from('mcm'); 		$this->db->where('kredit !=','0.00');		$this->db->order_by('id_mcm', 'asc');		$this->db->limit($num, $offset);		return $this->db->get();	}
	function getItem($num=0, $offset=0)
	{			$this->db->flush_cache();		$this->db->select('distinct(id_mcm),tanggal,uraian,namafile');		$this->db->from('mcm_post');		$this->db->order_by('id_mcm','desc');		$this->db->limit($num, $offset);		return $this->db->get();
	}		function getItem_count()	{			$this->db->flush_cache();		$this->db->select('COUNT(distinct(id_mcm)) as jml');		$this->db->from('mcm_post');  		return $this->db->get();	}		function getItem_unposting()	{			$this->db->flush_cache();$query = "select count(id) as jml from mcm where status_posting != '1' and kredit != '0.00'";				$this->db->flush_cache();		return $this->db->query($query); 	}		function getItem2($num=0, $offset=0)	{			$this->db->flush_cache();		$this->db->select('jurnal.GLID,					jurnal.TANGGAL,					jurnal.KETERANGAN,					penerimaan_kas.jumlah,					penerimaan_kas.mcm,					mcm.parawaba,					master_parawaba.nama,					jurnal.GLID_PARENT');		$this->db->from('jurnal');		$this->db->join('penerimaan_kas','penerimaan_kas.glid=jurnal.GLID_PARENT');		$this->db->join('mcm','penerimaan_kas.mcm = mcm.id');		$this->db->join('master_parawaba','mcm.parawaba=master_parawaba.id');		$this->db->where('penerimaan_kas.status_posting','1');		$this->db->order_by('tanggal, glid, glid_parent, parawaba');		$this->db->limit($num, $offset);		return $this->db->get();	}		function getItem2_count()	{			$this->db->flush_cache();		$this->db->select('COUNT(jurnal.GLID) as jml');		$this->db->from('jurnal');		$this->db->join('penerimaan_kas','penerimaan_kas.glid=jurnal.GLID_PARENT');		$this->db->join('mcm','penerimaan_kas.mcm = mcm.id');		$this->db->join('master_parawaba','mcm.parawaba=master_parawaba.id');		$this->db->where('penerimaan_kas.status_posting','1');  		return $this->db->get();	}
	 function get_detail_mcm_list2($glid){		$this->db->flush_cache();		$this->db->select('*');		$this->db->from('jurnal');		$this->db->join('detail_jurnal','detail_jurnal.GLID=jurnal.GLID','inner');		$this->db->join('master_akun','master_akun.akunid=detail_jurnal.AKUNID','left');		$this->db->where('jurnal.GLID', $glid);		return $this->db->get();	}		 function get_detail_mcm_list($glid){		$this->db->flush_cache();		$this->db->select('jurnal.*, detail_jurnal.*, master_akun.nakun, 					penerimaan_kas.jumlah,					penerimaan_kas.mcm,					mcm.parawaba,					master_parawaba.nama');		$this->db->from('jurnal');		$this->db->join('detail_jurnal','detail_jurnal.GLID=jurnal.GLID','inner');		$this->db->join('master_akun','master_akun.akunid=detail_jurnal.AKUNID','left');		$this->db->join('penerimaan_kas','penerimaan_kas.glid=jurnal.GLID_PARENT');		$this->db->join('mcm','penerimaan_kas.mcm = mcm.id');		$this->db->join('master_parawaba','mcm.parawaba=master_parawaba.id');		$this->db->where('jurnal.GLID', $glid);		return $this->db->get();	}		function get_mcm_from_jurnal($glid){		$this->db->flush_cache();		$this->db->select('	DISTINCT(penerimaan_kas.mcm) ');		$this->db->from('jurnal');		$this->db->join('detail_jurnal','detail_jurnal.GLID=jurnal.GLID','inner');		$this->db->join('master_akun','master_akun.akunid=detail_jurnal.AKUNID','left');		$this->db->join('penerimaan_kas','penerimaan_kas.glid=jurnal.GLID_PARENT');		$this->db->join('mcm','penerimaan_kas.mcm = mcm.id');		$this->db->join('master_parawaba','mcm.parawaba=master_parawaba.id');		$this->db->where('jurnal.GLID', $glid);		return $this->db->get();	}		function get_mcm_from_penerimaan($glid){		$query = "SELECT * from penerimaan_kas where glid = '$glid' 					";				$this->db->flush_cache();		return $this->db->query($query);	}	
	function getItemBy($num=0, $offset=0)
	{
		$this->db->flush_cache();
		$this->db->select('mcm.id, mcm.id_mcm, mcm.tanggal, mcm.tanggal_val, mcm.rincian_transaksi, mcm.no_referensi, mcm.debet, mcm.kredit, mcm.saldo, mcm.parawaba, mcm.pendapatan, mcm.uraian, mcm.status_posting');		$this->db->from('mcm');		$this->db->where('status_posting','0');		$this->db->where('kredit !=','0.00');
		$this->db->ORDER_BY("tanggal", "ASC");		$this->db->limit($num, $offset);		return $this->db->get();
	}		function getItemByID($id)	{		$this->db->flush_cache();		$this->db->select('*');		$this->db->from('mcm');				$this->db->where('id', $id);		return $this->db->get();	}		function getItemByCount()	{		$this->db->flush_cache();		$this->db->select('count(mcm.id) as jml');		$this->db->from('mcm');		$this->db->where('status_posting','0');		$this->db->where('kredit !=','0.00');		$this->db->ORDER_BY("tanggal", "ASC");		return $this->db->get();	}	 	
	function getItemByDate($periode_awal,$periode_akhir)	{		$this->db->select('mcm.id, mcm.id_mcm, mcm.tanggal, mcm.tanggal_val, mcm.rincian_transaksi, mcm.no_referensi, mcm.debet, mcm.kredit, mcm.saldo, mcm.parawaba, mcm.pendapatan, mcm.uraian, mcm.status_posting');		$this->db->from('mcm');		$this->db->where("tanggal BETWEEN '" . $periode_awal . "' AND '" . $periode_akhir .  "'");				$this->db->where("kredit != '0.00'");		$this->db->ORDER_BY("tanggal", "ASC");		return $this->db->get();	}		function getItemByDate_Count($periode_awal,$periode_akhir)	{		$this->db->select('COUNT(mcm.id) as jml');		$this->db->from('mcm');		$this->db->where("tanggal BETWEEN '" . $periode_awal . "' AND '" . $periode_akhir .  "'");				$this->db->where("kredit != '0.00'"); 		return $this->db->get();	}		function getForReportByDate($periode_awal,$periode_akhir)	{		$query = "SELECT					mcm.id,					mcm.id_mcm,					mcm.tanggal,					mcm.tanggal_val,					mcm.rincian_transaksi,					mcm.no_referensi,					mcm.debet,					mcm.kredit, 					mcm.uraian,					mcm.parawaba,					master_parawaba.nama,					mcm.pendapatan,					master_akun.nakun,					mcm.status_posting					FROM					mcm					left JOIN master_parawaba ON mcm.parawaba = master_parawaba.id					left JOIN master_akun ON master_akun.akunid = mcm.pendapatan					where status_posting = 1 and					mcm.tanggal BETWEEN '$periode_awal' AND '$periode_akhir' ";				$this->db->flush_cache();		return $this->db->query($query);	}	 	
	function insert_mcm_post($data)
	{
		$this->db->flush_cache();
		$this->db->insert('mcm_post', $data);		$this->db->insert('mcm_post_backup', $data);
	}
	function insert_mcm($data)	{		$this->db->flush_cache();		$this->db->insert('mcm', $data);		$this->db->insert('mcm_backup', $data);	}				function insert_penerimaan($data){		$this->db->flush_cache();		$this->db->insert('penerimaan_kas', $data);	}		function insert_detail_penerimaan($data){		$this->db->flush_cache();		$this->db->insert('detail_penerimaan_kas', $data);	}			function insert($data)	{		$this->db->flush_cache();		$this->db->insert('jurnal', $data);	}	function insert_detailJ($data)	{		$this->db->flush_cache();		$this->db->insert('detail_jurnal', $data);	}		function update($id, $data)	{		$this->db->flush_cache();		$this->db->where('so_no', $id);		$this->db->update('penjualan', $data);	}		function get_detail_penjualan($so_no){		$this->db->flush_cache();		$this->db->select('penjualan.so_no,						penjualan.id_penjualan,						detail_penjualan.id_barang,						detail_penjualan.harga,						detail_penjualan.qty,						detail_penjualan.total,						barang.nama_barang,						barang.id_kategori,						kategori.akunid,						kategori.kategori');		$this->db->from('penjualan');		$this->db->join('detail_penjualan', 'penjualan.id_penjualan = detail_penjualan.id_penjualan');		$this->db->join('barang', 'barang.id_barang = detail_penjualan.id_barang');		$this->db->join('kategori', 'kategori.id_kategori = barang.id_kategori');		$this->db->where('penjualan.so_no',$so_no);		return $this->db->get();	}		function get_glid(){		$this->db->flush_cache();		//$query1 = $this->db->query("SHOW TABLE STATUS LIKE 'detail_jurnal'");		$query1 = $this->db->query("SHOW TABLE STATUS LIKE 'jurnal'");		// ngambil no autoincrement dan akhirnya jadi-> '000x'		$tmp_no = $query1->row()->Auto_increment;		$no = str_pad($tmp_no, 4, '0', STR_PAD_LEFT);		return 'AR/'.substr(date('Y'),2,2).date('md').'/'.$no;		//return 'KKM-'.$no;	}		function get_glid_penerimaan(){		$this->db->flush_cache();		$query1 = $this->db->query("SHOW TABLE STATUS LIKE 'penerimaan_kas'");		// ngambil no autoincrement dan akhirnya jadi-> '000x'		$tmp_no = $query1->row()->Auto_increment;		$no = str_pad($tmp_no, 4, '0', STR_PAD_LEFT);		return 'BKM/'.substr(date('Y'),2,2).date('md').'/'.$no;		//return 'KKM-'.$no;	}	
	function update_mcm($id, $data)
	{
		$this->db->flush_cache();
		$this->db->where('id', $id);
		$this->db->update('mcm', $data);
	}		function update_penerimaan_1($id, $data)
	{		$this->db->flush_cache();		$this->db->where('glid', $id);		$this->db->where('debet != 0');		$this->db->update('detail_penerimaan_kas', $data);
	}		function update_penerimaan_4($id, $data)
	{		$this->db->flush_cache();		$this->db->where('glid', $id);		$this->db->where('kredit != 0');		$this->db->update('detail_penerimaan_kas', $data);
	}
	
	function delete($id)
	{
		$this->db->flush_cache();
		$this->db->delete('mcm_post', array('id_mcm' => $id));		$this->db->delete('mcm', array('id_mcm' => $id));
	}		function get_item_list_unposting_count()	{			$this->db->flush_cache();		$this->db->select('COUNT(penerimaan_kas.glid) as jml, 					penerimaan_kas.tanggal,					penerimaan_kas.ket_transaksi,					penerimaan_kas.jumlah,					master_parawaba.nama');		$this->db->from('penerimaan_kas');		$this->db->join('mcm', 'penerimaan_kas.mcm = mcm.id','inner');		$this->db->join('master_parawaba', 'mcm.parawaba = master_parawaba.id','inner');		$this->db->ORDER_BY("glid", "ASC");		$this->db->where("penerimaan_kas.status_posting",'0');		return $this->db->get();	}	
	function get_item_list_unposting($num=0, $offset=0){			//$this->db->flush_cache();		$this->db->flush_cache();		$this->db->select(' 					penerimaan_kas.glid,					penerimaan_kas.tanggal,					penerimaan_kas.ket_transaksi,					penerimaan_kas.jumlah,					master_parawaba.nama');		$this->db->from('penerimaan_kas');		$this->db->join('mcm', 'penerimaan_kas.mcm = mcm.id','inner');		$this->db->join('master_parawaba', 'mcm.parawaba = master_parawaba.id','inner');		$this->db->ORDER_BY("glid", "ASC");		$this->db->where("penerimaan_kas.status_posting",'0');		$this->db->limit($num, $offset);				return $this->db->get();  	}	function get_detail_mcm($glid){			$query = "SELECT					penerimaan_kas.glid,					penerimaan_kas.tanggal,					penerimaan_kas.ket_transaksi,					detail_penerimaan_kas.akunid,					detail_penerimaan_kas.debet,					detail_penerimaan_kas.kredit,					master_akun.nakun					FROM					penerimaan_kas					INNER Join detail_penerimaan_kas ON detail_penerimaan_kas.glid = penerimaan_kas.GLID					LEFT Join master_akun ON master_akun.akunid = detail_penerimaan_kas.akunid 						where penerimaan_kas.glid='" . $glid . "'";				$this->db->flush_cache();		return $this->db->query($query);	}			function get_item_penerimaan_kas($glid){		$this->db->flush_cache();		$this->db->select('*');		$this->db->from('penerimaan_kas');		$this->db->where('penerimaan_kas.glid', $glid);		return $this->db->get();	}		function get_detail_item_penerimaan_kas($glid){		$this->db->flush_cache();		$this->db->select('*');		$this->db->from('detail_penerimaan_kas');		$this->db->where('detail_penerimaan_kas.glid', $glid);		return $this->db->get();	}		function get_glid_jurnal(){		$this->db->flush_cache();		//$query1 = $this->db->query("SHOW TABLE STATUS LIKE 'detail_jurnal'");		$query1 = $this->db->query("SHOW TABLE STATUS LIKE 'jurnal'");		// ngambil no autoincrement dan akhirnya jadi-> '000x'		$tmp_no = $query1->row()->Auto_increment;		$no = str_pad($tmp_no, 4, '0', STR_PAD_LEFT);		return 'AR/'.substr(date('Y'),2,2).date('md').'/'.$no;		//return 'KKM-'.$no;	}			function insert_j($data)	{		$this->db->flush_cache();		$this->db->insert('jurnal', $data);	}		function insert_detail_j($data)	{		$this->db->flush_cache();		$this->db->insert('detail_jurnal', $data);	}		function update_posting_penerimaan_kas($glid,$data){				$this->db->flush_cache();		$this->db->where('glid', $glid);		$this->db->update('penerimaan_kas', $data);	}		function get_for_edit_mcm($id)	{		$query = "select DISTINCT(mcm.id)as id2, mcm.*,		(select detail_penerimaan_kas.akunid 		from mcm 		join penerimaan_kas on mcm.id = penerimaan_kas.mcm 		join detail_penerimaan_kas on detail_penerimaan_kas.glid = penerimaan_kas.glid 		where detail_penerimaan_kas.debet != '0' and  mcm.id = $id  ) as kepala_1,				(select  master_akun.nakun		from master_akun 	 	where master_akun.akunid = (select detail_penerimaan_kas.akunid 		from mcm 		join penerimaan_kas on mcm.id = penerimaan_kas.mcm 		join detail_penerimaan_kas on detail_penerimaan_kas.glid = penerimaan_kas.glid 		where detail_penerimaan_kas.debet != '0' and  mcm.id = $id)		) as NAKUN_1,				(select master_akun.nakun		from master_akun		where master_akun.akunid = mcm.pendapatan) as NAKUN_4,				penerimaan_kas.glid		from mcm		join   penerimaan_kas on mcm.id = penerimaan_kas.mcm		join detail_penerimaan_kas on detail_penerimaan_kas.glid = penerimaan_kas.glid		where mcm.id =$id";		$this->db->flush_cache();		return $this->db->query($query);	}
}